﻿using Microsoft.AspNetCore.Http;
using System.Net;

namespace Soul.IdentityServer.Hosting
{
    public abstract class EndpointResult
    {
        public abstract Task ExecuteAsync(HttpContext context);
    }

    public class JsonEndpointResult : EndpointResult
    {
        public object Data { get;}

        public JsonEndpointResult(object data)
        {
            Data = data;
        }

        public override async Task ExecuteAsync(HttpContext context)
        {
            context.Response.StatusCode = (int)HttpStatusCode.OK;
            await context.Response.WriteAsJsonAsync(Data);
        }
    }

    public class BadRequestEndpointResult : EndpointResult
    {
        public string Error { get; set; }
        public string? ErrorDescription { get; set; }

        public BadRequestEndpointResult(string error, string? errorDescription)
        {
            Error = error;
            ErrorDescription = errorDescription;
        }

        public override async Task ExecuteAsync(HttpContext context)
        {
            context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
            await context.Response.WriteAsJsonAsync(new
            {
                error = Error,
                error_description = ErrorDescription
            });
        }
    }
}
